// pages/classify/classify.js
const
    app = getApp(),
    globalData = app.globalData,
    api = require('../../api/index');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        systemInfo: {},
        menuInfo: {},   
        isIpx: globalData.isIpx,
        visible: false,
        list: [],
        listLength:"",
        typeLength:"",
        status: 0,
        filter: {
            keywords: "",
            brandId: "",
            categoryId: "",
            types: [1, 2, 3],
            goodsOnState: 1,
            goodsSortByEnum: 2,
            // currenetPageIndex: 1,
            // pageSize: 10,
            recordCount: 0
        },
        active: 1,
        typeList: [{ Title: '全部', Id: '' }],
        imageBaseUri: globalData.imageBaseUri,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(this.data.isIpx, 'isIpx')
        this.reload()
    },

    reload() {
        this.getTypes();  
    },
    toDetail(e) {
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: `/pages/product/detail/detail?id=${id}`,
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let menuInfo = wx.getMenuButtonBoundingClientRect();
    let systemInfo = wx.getSystemInfoSync();
    console.log(systemInfo, menuInfo);
    this.setData({
      systemInfo,
      menuInfo,
    });
},

    /**
     * 页面上拉触底事件的处理函数
     */
    // onReachBottom: function ()  {
    //     let that=this
    //     if (this.data.hasMore) {
    //             // this.setData({
    //             //     'filter.currenetPageIndex': ++this.data.filter.currenetPageIndex
    //             // });
    //             this.getGoodsList();
               
    //     }else{
    //         console.log("结束",this.data.active,this.data.typeLength);
    //         console.log(this.data.typeList.length,this.data.active,"分类数量");
    //         if (this.data.typeList.length-1 ==this.data.active) {
    //             that.setData({
    //                 active:this.data.typeList.length-1
    //             })
    //            return
    //         }
    //             if(this.data.active>=this.data.typeLength){ 
    //                 that.setData({
    //                     active:that.data.active+1
    //                 })
    //                 that.data.typeList.forEach((item,index)=>{
    //                     if(index==that.data.active){
    //                        that.pullType(item)
    //                     }
    //                 }) 
    //             }
    //     }
    //         // this.changeType()
    // },
   

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    showMask() {
        this.setData({
            visible: true
        })
    },
    hideMask() {
        this.setData({
            visible: false
        })
    },
    toSearch() {
        wx.navigateTo({
            url: '/pages/search/search',
        })
    },
    // 返回首页
    goback(){
        wx.switchTab({
            url: '/pages/index/index'
          })
    },
    // 获取商品列表
    async getGoodsList() {
        this.setData({
            "filter.goodsSortByEnum":2
        })
        let obj = this.data.filter;
        console.log(obj);
        wx.showLoading({
            title: '内容加载中',
        })
        try {
            let res = await api.goods.getGoodsPages(obj),
                data = res.data.ResData || {};
            console.log(res, '商品列表')
            if (this.data.filter.currenetPageIndex > 1) {
                data.Items = this.data.list.concat(data.Items);
            }

            if (data.Items&&data.Items.length) {
                data.Items.map(item => {
                    if (item.MarketingTypes.toString().indexOf('2') != -1) {
                        item.isSeckill = true;
                    } else {
                        item.isSeckill = false;
                    }
                    if (item.MarketingTypes.toString().indexOf('3') != -1) {
                        item.isGroup = true;
                    } else {
                        item.isGroup = false;
                    }
                })
            }


            this.setData({
                list: data.Items ? data.Items : [],
                hasMore: data.TotalPages == 0 ? false : data.CurrentPage == data.TotalPages ? false : true,
                status: 2
            })

        } catch (e) {
            console.log(e, '获取商品类别失败')
            this.setData({
                status: 1
            })
        } finally {
            wx.hideLoading()
        }
    },

    // 获取商品分类
    async getTypes() {
        try {
            let res = await api.goods.goodsType(),
                data = res.data.ResData || [];
            // arr = [{ Title: '全部', Id: '' }];
            // let obj = { Title: '全部', Id: '' };
            // data = arr.push(data);
            this.setData({
                typeList: this.data.typeList.concat(data),
                status: 2,
                typeLength:this.data.typeList.length,
            })
            this.data.filter.categoryId=this.data.typeList[1].Id
            console.log(this.data.filter);
            this.getGoodsList()
        } catch (e) {
            console.log(e, '获取商品分类列表失败')
        }
    },
    // 下拉切换分类
    async pullType(item){
        this.setData({
            filter: {
                keywords: "",
                brandId: "",
                categoryId: item.id,
                types: [1, 2, 3],
                goodsOnState: 1,
                goodsSortByEnum: 1,
                // currenetPageIndex: 1,
                // pageSize: 10,
                recordCount: 0
            },
            list: []
        })
        // this.hideMask();
        this.getGoodsList();
        wx.pageScrollTo({ scrollTop: 0 });
    },
    // 切换分类
    async changeType(e) {
        let id = e.currentTarget.dataset.id,
            index = e.currentTarget.dataset.index;
        console.log(e);
        this.setData({
            active: index,
            filter: {
                keywords: "",
                brandId: "",
                categoryId: id,
                types: [1, 2, 3],
                goodsOnState: 1,
                goodsSortByEnum: 1,
                // currenetPageIndex: 1,
                // pageSize: 10,
                recordCount: 0
            },
            list: []
        })
        this.hideMask();
        this.getGoodsList();
        wx.pageScrollTo({ scrollTop: 0 });
    }
})